<!--
 * @Author       : xiaowu wwl093000@163.com
 * @Date         : 2023-06-21 15:28:57
 * @LastEditTime : 2023-06-25 17:20:16
 * @LastEditors  : xiaowu wwl093000@163.com
 * @FilePath     : /erp-admin-ui/src/views/persongather/components/dialogList.vue
 * @Description  : -
-->
<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item v-if="certificateInfo" label="是否已绑定资质" prop="isBindingQualifications">
        <el-select size="small" v-model="queryParams.isBindingQualifications" placeholder="请选择是否已绑定资质信息"
                   clearable filterable>
          <el-option v-for="(dict,index) in dict.type.is_bind_qualification_info" :key="index"
                     :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="商品类目" prop="categoryId">
        <!--        <el-cascader style="width: 300px;" filterable v-model="queryParams.categoryId" placeholder="请选择类目" :options="categoryList"-->
        <!--                     :props="{ value: 'id', label: 'name', emitPath: false }" :show-all-levels="false" clearable>-->
        <!--        </el-cascader>-->
        <treeselect
          style="width: 300px;"
          v-model="queryParams.categoryId"
          :options="categoryList"
          :normalizer="normalizer"
          placeholder="请选择商品类目"
        />
      </el-form-item>
      <el-form-item label="商品标题" prop="subject">
        <el-input
          v-model="queryParams.subject"
          placeholder="请输入商品标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品Id" prop="id">
        <el-input
          v-model="queryParams.id"
          @keyup.enter.native="handleQuery"
          placeholder="产品Id搜索"
          clearable
        >
        </el-input>
      </el-form-item>
      <div v-if="!certificateInfo">
        <el-form-item label="商品链接" prop="collectUrl">
          <el-input
            v-model="queryParams.collectUrl"
            placeholder="请输入商品链接"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="采集商品" prop="isCollect">
          <el-select v-model="queryParams.isCollect" placeholder="是否为采集商品" clearable>
            <el-option v-for="dict in collectList" :key="dict.value" :label="dict.label"
                       :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="编辑" prop="isEdit">
          <el-select v-model="queryParams.isEdit" placeholder="是否已编辑" clearable>
            <el-option v-for="dict in editList" :key="dict.value" :label="dict.label"
                       :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="优化" prop="isOptimize">
          <el-select v-model="queryParams.isOptimize" placeholder="是否已优化" clearable>
            <el-option v-for="dict in optimizeList" :key="dict.value" :label="dict.label"
                       :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="运费试算" prop="isFreight">
          <el-select v-model="queryParams.isFreight" placeholder="是否已运费试算" clearable>
            <el-option v-for="dict in freightList" :key="dict.value" :label="dict.label"
                       :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="翻译" prop="isTranslate">
          <el-select v-model="queryParams.isTranslate" placeholder="是否已翻译" clearable>
            <el-option v-for="dict in translateList" :key="dict.value" :label="dict.label"
                       :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="发布" prop="isGrounding">
          <el-select v-model="queryParams.isGrounding" placeholder="是否已发布" clearable>
            <el-option v-for="dict in groundingList" :key="dict.value" :label="dict.label"
                       :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="发布测试状态：" prop="isUploadTest" label-width="120px">
          <el-select v-model="queryParams.isUploadTest" clearable placeholder="请选择发布测试状态">
            <el-option v-for="item in dict.type.test_release_status" :key="item.value" :label="item.label"
                       :value="item.value"></el-option>
          </el-select>
        </el-form-item>
      </div>
      <el-form-item label="skuCode" prop="skuCode">
        <el-input v-model="queryParams.skuCode" placeholder="skuCode搜索"></el-input>
      </el-form-item>
      <el-form-item label="品牌" prop="brandId">
        <el-select v-model="queryParams.brandId" placeholder="请选择品牌" clearable size="small">
          <el-option
            v-for="dict in brandList"
            :key="dict.value"
            :label="dict.lable"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8" v-if="certificateInfo">
      <el-button v-if="queryParams.isBindingQualifications == '0'" type="primary" plain size="mini"
                 :disabled="multiple" single @click="handleAdd">绑定
      </el-button>
      <el-button v-if="queryParams.isBindingQualifications =='1'" type="danger" plain size="mini" :disabled="multiple"
                 @click="handleDelete">解绑
      </el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="collectProductList" @selection-change="handleSelectionChange" stripe border>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="产品Id" align="center" prop="id" width="100"/>
      <el-table-column show-overflow-tooltip label="商品标题" align="center" prop="subject"/>
      <el-table-column label="商品图" align="center" prop="white" width="100">
        <template slot-scope="scope">
          <imagePreview
            :src="scope.row.white"
            width="60px"
            height="60px"
          />
        </template>
      </el-table-column>
      <el-table-column label="商品类目" align="center" prop="categoryName" width="100"/>
      <el-table-column label="商品标签" align="center" prop="classifyLabel">
        <template slot-scope="scope">
          <el-tag v-for="(item, index) in goodsTagLabel(scope.row.expand)" :key="index" style="margin: 5px">
            {{ item }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="采集链接" align="center" prop="collectUrl">
        <template slot-scope="scope">
          <a :href="scope.row.collectUrl" style="color: #1890ff;" target="_blank">{{ scope.row.collectUrl }}</a>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :pageSizes="[5, 10, 20]"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listCollectProduct,
  getSelect,
  getBrand
} from "@/api/gather/collectProduct";
import {
  selectlistCollectProductCategory
} from "@/api/gather/collectProductCategory";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {bindingQualifications, selectCollectList, unBindingQualifications} from "@/api/certificate/certificate"

export default {
  name: "GoodsList",
  dicts: ['test_release_status', 'is_bind_qualification_info'],
  props: {
    certificateInfo: {}
  },
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采集商品表格数据
      collectProductList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        categoryId: undefined,
        subject: undefined,
        collectUrl: undefined,
        isCollect: undefined,
        isEdit: undefined,
        isOptimize: undefined,
        isFreight: undefined,
        isTranslate: undefined,
        collectUserId: undefined,
        id: undefined,
        skuCode: undefined,
        brandId: undefined,
        type: '1',
        isBindingQualifications: 1
      },
      // 标签列表
      tagList: [],
      // 类目列表
      categoryList: [],
      // 是否为采集商品
      collectList: [
        {
          value: 0,
          label: '是'
        },
        {
          value: 1,
          label: '否'
        }
      ],
      // 编辑列表
      editList: [
        {
          value: 0,
          label: '否'
        },
        {
          value: 1,
          label: '是'
        }
      ],
      // 优化列表
      optimizeList: [
        {
          value: 0,
          label: '否'
        },
        {
          value: 1,
          label: '是'
        }
      ],
      // 发布列表
      groundingList: [
        {
          value: 0,
          label: '否'
        },
        {
          value: 1,
          label: '是'
        }
      ],
      // 运费试算列表
      freightList: [
        {
          value: 0,
          label: '未运费试算'
        },
        {
          value: 1,
          label: '等待运费试算'
        },
        {
          value: 2,
          label: '运费试算中'
        },
        {
          value: 3,
          label: '已运费试算'
        }
      ],
      // 翻译列表
      translateList: [
        {
          value: 0,
          label: '未翻译'
        },
        {
          value: 1,
          label: '等待翻译'
        },
        {
          value: 2,
          label: '翻译中'
        },
        {
          value: 3,
          label: '已翻译'
        },
        {
          value: 4,
          label: '无需翻译'
        }
      ],
      // 品牌列表
      brandList: []
    };
  },
  computed: {
    // 标签
    goodsTagLabel() {
      if (this.tagList.length > 0) {
        return (row) => {
          if (row && row.classifyLabel) {
            try {
              const defaultList = row.classifyLabel.split(',').filter(item => !!item)
              const classifyLabelList = []
              for (let i = 0; i < defaultList.length; i++) {
                for (let k = 0; k < this.tagList.length; k++) {
                  if (defaultList[i] == this.tagList[k].value) {
                    classifyLabelList.push(this.tagList[k].lable)
                  }
                }
              }
              return classifyLabelList
            } catch (e) {
              return []
            }
          } else return []
        }
      } else return []
    }
  },
  created() {
    console.log('certificateInfo', this.certificateInfo)
    this.getTagList();
    this.getGoodsCategory();
    this.getBrandList();
    if (this.certificateInfo) {
      // this.isformCertificateInfo = true
      this.queryParams = this.certificateInfo
      this.getqualificationList()
    } else {
      this.getList();
    }

  },
  methods: {
    // 导入到公共采集库（绑定）
    handleAdd() {
      const ids = (this.ids).toString();
      if (ids) {
        this.$modal.confirm('是否将选中的产品绑定资质？').then(() => {
          const params = {
            collectProductId: ids,
            programId: this.certificateInfo.qualificationsId
          }
          bindingQualifications(params).then(res => {
            this.getqualificationList();
            this.isImport = false;
            this.$modal.msgSuccess(res.msg);
          })
        })
      } else {
        this.$modal.msgError("请先选择商品！");
      }
    },
    // 解除绑定
    handleDelete() {
      const ids = (this.ids).toString();
      if (ids) {
        this.$modal.confirm('是否将选中的产品解除绑定资质？').then(() => {
          const params = {
            collectProductId: ids,
            programId: this.certificateInfo.qualificationsId
          }
          unBindingQualifications(params).then(res => {
            this.getqualificationList();
            this.isImport = false;
            this.$modal.msgSuccess(res.msg);
          })
        })
      } else {
        this.$modal.msgError("请先选择商品！");
      }
    },
    // 获取证书相关产品列表
    getqualificationList() {
      this.loading = true;
      selectCollectList(this.queryParams).then(res => {
        this.collectProductList = res.rows;
        this.total = res.total;
        this.loading = false;
      })
    },
    idsChange(e) {
      this.ids = e;
    },
    /** 查询采集商品列表 */
    getList() {
      this.loading = true;
      listCollectProduct(this.queryParams).then(response => {
        this.collectProductList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 标签列表
    getTagList() {
      getSelect().then(res => {
        this.tagList = res.data
      });
    },
    // 商品类目
    getGoodsCategory() {
      selectlistCollectProductCategory().then(response => {
        this.categoryList = this.handleTree(response.data, "id", "parentId");
      });
    },
    /** 转换采集商品类目数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.certificateInfo) {
        // this.queryParams = this.certificateInfo
        this.getqualificationList()
      } else {
        this.getList();
      }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 获取品牌选择列表
    getBrandList() {
      getBrand().then(res => {
        this.brandList = res.data
      });
    }
  }
};
</script>

<style lang="scss">

</style>

